package com.xjj;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhanghejie
 */
@SpringBootApplication
@MapperScan("com.xjj.**.dao")
@EnableSwagger2
public class ApiApplication extends SpringBootServletInitializer{
	
	/**
	 * 导出war在外部tomcat使用时, 不能使用main函数运行, 需要配置此项
	 */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(ApiApplication.class);
    }

    /**
     * IDE运行时 运行此函数
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
    }


    private List<Predicate<String>> defaults = new ArrayList<Predicate<String>>();//
    private List<Predicate<String>> test = new ArrayList<Predicate<String>>();// 模块
    private void loadUri() {
        test.add(PathSelectors.regex("/test.*"));
        defaults.addAll(test);
    }
    @Bean
    public Docket userApi() {
        loadUri();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xjj.api"))
                .paths(Predicates.or(defaults))
                .build();
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("JEEXJJ")
                .description("welcome to jeexjj")
                .build();
    }
}


